package com.red.work.demo;

import com.red.work.model.BuildData;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.util.StringUtils;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

/**
 * 抓取安居客访客数据脚本
 */
public class ChromeAnjuke {

    /**
     * step1 第一次点击之后会出现扫描登陆的情况，先进行扫码登陆，关闭程序
     * step2 修改下面的 URL 地址，该地址就是需要爬的页面
     * step3 程序运行结束后就会有一个输出文件，数字越大是越新的文件
     */

    // 只需修改这个地址
    private final static String url = "https://new.anjuke.com/sz/adminnew/Crm/Sale/loupan/cluelist/W0QQloupan_idZ478758";

    public static void main(String[] args)  throws Exception {

        String path = getPath();

        System.setProperty("webdriver.chrome.driver", path + "src/main/java/com/red/work/chrome/chromedriver");

        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--user-data-dir=" + getUserPath() + "/Library/Application Support/Google/Chrome/Default");

        //实例化ChromeDriver对象
        WebDriver driver = new ChromeDriver(chromeOptions);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        driver.get("https://new.anjuke.com/sh/adminnew/Crm/User/Login");
        driver.findElement(By.xpath("//*[@id=\"ajk-btn-login\"]")).click();
        Thread.sleep(1000);

        //打开指定网站
        driver.get("https://new.anjuke.com/sz/W0QQidZAF013");
        Thread.sleep(1000);

        driver.get(url);

        // 获取第一页数据
        List<BuildData> pageData = getPageData(driver);

        // 进入下一页
        WebElement nextPage = getNextPage(driver, pageData);
        while (nextPage != null) {
            nextPage = getNextPage(driver, pageData);
        }

        // 创建excel文件
        createExcel(pageData, path);

        Thread.sleep(10000);
        driver.close();
    }

    private static WebElement getNextPage(WebDriver driver, List<BuildData> pageData) {
        WebElement webElement = null;
        try {
            webElement = driver.findElement(By.xpath("//*[@id=\"main\"]/div[1]/div[3]/form/div/div/div[2]/a[14]"));
            webElement.click();
            Thread.sleep(2000);
            List<BuildData> data = getPageData(driver);
            if (data.size() < 20) {
                webElement = null;
            }
            pageData.addAll(data);
        } catch (Exception e) {
            System.err.println("getNextPage error ===================>>>" + e);
        }
        return webElement;
    }

    private static List<BuildData> getPageData(WebDriver driver) {
        List<BuildData> buildDataList = new ArrayList<>();
        for (int i = 1; i <= 20; i++) {
            try {
                String phoneAndArea = driver.findElement(By.xpath("//*[@id=\"main\"]/div[1]/div[3]/form/div/div/table/tbody/tr[" + i + "]/td[1]")).getText();
                String phone = phoneAndArea, area = "";
                if (!StringUtils.isEmpty(phoneAndArea) && phoneAndArea.contains("\n")) {
                    phone = StringUtils.trimAllWhitespace(phoneAndArea.split("\n")[0]);
                    area = StringUtils.trimAllWhitespace(phoneAndArea.split("\n")[1]);
                }
                String time = getTimeText(driver, i);
                String source = getSourceText(driver, i);

                BuildData data = new BuildData();
                data.setPhone(phone);
                data.setArea(area);
                data.setTime(time);
                data.setSource(source);
                buildDataList.add(data);
                System.out.println("number" + i + "\t" + phone + "\t" + area + "\t" + time + "\t" + source);
            } catch (Exception e ) {
                System.err.println("getPageData error ===================>>>" + e);
                return buildDataList;
            }
        }
        return buildDataList;
    }

    private static void createExcel(List<BuildData> pageData, String path) throws Exception {
        //创建HSSFWorkbook对象
        HSSFWorkbook wb = new HSSFWorkbook();
        //创建HSSFSheet对象
        HSSFSheet sheet = wb.createSheet("楼盘咨询数据");
        //创建HSSFRow对象
        HSSFRow row = sheet.createRow(0);
        //创建单元格并设置单元格内容
        row.createCell(0).setCellValue("手机");
        row.createCell(1).setCellValue("地区");
        row.createCell(2).setCellValue("时间");
        row.createCell(3).setCellValue("来源");

        for (int i = 0,size = pageData.size(); i < size; i++) {
            HSSFRow dataRow = sheet.createRow(i + 1);
            //创建HSSFCell对象
            BuildData buildData = pageData.get(i);
            dataRow.createCell(0).setCellValue(buildData.getPhone());
            dataRow.createCell(1).setCellValue(buildData.getArea());
            dataRow.createCell(2).setCellValue(buildData.getTime());
            dataRow.createCell(3).setCellValue(buildData.getSource());
        }
        //输出Excel文件
        FileOutputStream output = new FileOutputStream(path + System.currentTimeMillis() +"output.xls");
        wb.write(output);
        output.flush();
    }

    private static String getTimeText(WebDriver driver, int i) {
        try {
            return driver.findElement(By.xpath("//*[@id=\"main\"]/div[1]/div[3]/form/div/div/table/tbody/tr[" + i + "]/td[2]/div[1]/div")).getText();
        } catch (Exception e) {
            System.err.println("getTimeText error ===================>>>" + e);
            return "";
        }
    }

    private static String getSourceText(WebDriver driver, int i) {
        try {
            return driver.findElement(By.xpath("//*[@id=\"main\"]/div[1]/div[3]/form/div/div/table/tbody/tr[" + i + "]/td[2]/div[1]/p")).getText();
        } catch (Exception e) {
            System.err.println("getSourceText error ===================>>>" + e);
            return "";
        }
    }

    private static String getPath() {
        String path = ChromeAnjuke.class.getResource("").getPath();
        return path.replace("target/classes/com/red/work/demo/", "");
    }

    private static String getUserPath() {
        Properties props=System.getProperties();
        return props.getProperty("user.home");
    }

}
